package com.spzx.product.utils;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.service.ProductSkuService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class LoadSkuId implements InitializingBean {
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    ProductSkuService productSkuService;
    //会再当前类的对象初始化以后执行
    @Override
    public void afterPropertiesSet() throws Exception {
        //加载数据存到redis的bitmap中
//        redisTemplate.opsForValue().setb
        //1、查询sku表中所有的sku的id
        List<Long> skuIds = productSkuService.list(Wrappers.lambdaQuery(ProductSku.class)
                        .select(ProductSku::getId) //设置select后要查询的列！
                )
                .stream()
                .map(ProductSku::getId) //获取每个sku的id字段返回
                .collect(Collectors.toList());//将上面返回的id存到List集合中！
        //2、将skuId保存到bitmap中: 指定key存储所有skuid的bitmap数组 ，skuId当作偏移量，
        skuIds.forEach(skuId->{
            redisTemplate.opsForValue().setBit( "spzx:sku:ids",skuId , true);
        });
    }
}
